package com.example.musicapi.controller;

import com.example.musicapi.entity.User;
import com.example.musicapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //@RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
    @GetMapping("/get")
    @ResponseBody
    public List<User> getAll(@RequestParam(defaultValue = "0") int page,
                               @RequestParam(defaultValue = "100") int size,
                             @RequestParam(required = false) String key) {
        if(key!=null && !key.equals("")){
            return userService.search(page, size,key);
        }else{
            return userService.findAll(page, size);
        }
    }

    @GetMapping("/getOne")
    @ResponseBody
    public User get(@RequestParam String name){
        return userService.findById(name) ;
    }

    @PostMapping("/login")
    @ResponseBody
    public User login(@RequestParam String name,@RequestParam String psw,@RequestParam String type){
        User u=userService.findById(name);
        if(u==null){
            return null;
        }else if(psw.equals(u.getPassword()) &&  type.equals(u.getType())){
            return u;
        }else{
            return  null;
        }
    }


    @PostMapping("/update")
    public String update(String name, String psw, String email,
                         String type,@RequestParam(required = false) String avatarBase64){
        try{
            User u=new User();
            if(avatarBase64!=null && !avatarBase64.equals("")){
                byte[] avatarBytes = Base64.getDecoder().decode(avatarBase64);
                u=new User(name,psw,email,avatarBytes,type);
            }else{
                u=new User(name,psw,email,null,type);
            }
            userService.update(u);
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @PostMapping("/add")
    public String add(String name, String psw, String email,
                      String type,@RequestParam(required = false) String avatarBase64 ) {
        // 处理文件
        User u=new User();
        if(avatarBase64!=null && !avatarBase64.equals("")){
            byte[] avatarBytes = Base64.getDecoder().decode(avatarBase64);
            u=new User(name,psw,email,avatarBytes,type);
        }else{

            u=new User(name,psw,email,null,type);
        }
        if(userService.findById(name)!=null){
            return "此用户名已存在！";
        }
        u=userService.add(u);
        if(u!=null && u.getName()!=null)
            return "success";
        else{
            return "fail";
        }

    }


    @PostMapping("/delete")
    public String delete(String name) {
        try {
            userService.delete(name);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }
}